//-----------------------------------------------------------------------
// <copyright file="a_EmailFriendDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_EmailFriendDAOBase
    {
        #region Common methods
        public virtual a_EmailFriend Createa_EmailFriendFromReader(IDataReader reader)
        {
            a_EmailFriend item = new a_EmailFriend();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ParentID"))) item.ParentID = (int)reader["ParentID"];
                if (!reader.IsDBNull(reader.GetOrdinal("FriendName"))) item.FriendName = (string)reader["FriendName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("LastSentDate"))) item.LastSentDate = (DateTime)reader["LastSentDate"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_EmailFriend methods
            
        public virtual void Createa_EmailFriend(a_EmailFriend a_EmailFriend)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_EmailFriends_ins");
                
                database.AddInParameter(dbCommand, "@ParentID", DbType.Int32, a_EmailFriend.ParentID);
                database.AddInParameter(dbCommand, "@FriendName", DbType.String, a_EmailFriend.FriendName);
                database.AddInParameter(dbCommand, "@Email", DbType.AnsiString, a_EmailFriend.Email);
                database.AddInParameter(dbCommand, "@LastSentDate", DbType.DateTime, a_EmailFriend.LastSentDate);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_EmailFriend.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_EmailFriend methods
        
        public virtual void Updatea_EmailFriend(a_EmailFriend a_EmailFriend)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_EmailFriends_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_EmailFriend.ID);
                database.AddInParameter(dbCommand, "@ParentID", DbType.Int32, a_EmailFriend.ParentID);
                database.AddInParameter(dbCommand, "@FriendName", DbType.String, a_EmailFriend.FriendName);
                database.AddInParameter(dbCommand, "@Email", DbType.AnsiString, a_EmailFriend.Email);
                database.AddInParameter(dbCommand, "@LastSentDate", DbType.DateTime, a_EmailFriend.LastSentDate);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_EmailFriend methods
        public virtual void Deletea_EmailFriend(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_EmailFriends_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_EmailFriend methods
        
        public virtual a_EmailFriend Geta_EmailFriend(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_EmailFriends_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_EmailFriend a_EmailFriend =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_EmailFriend = Createa_EmailFriendFromReader(reader);
                        reader.Close();
                    }
                }
                return a_EmailFriend;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_EmailFriendList methods
        public virtual a_EmailFriendCollection Geta_EmailFriendList(a_EmailFriendColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_EmailFriends_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_EmailFriendCollection a_EmailFriendCollection = new a_EmailFriendCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_EmailFriend a_EmailFriend = Createa_EmailFriendFromReader(reader);
                        a_EmailFriendCollection.Add(a_EmailFriend);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_EmailFriendCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_EmailFriendCollection Geta_EmailFriendList(a_EmailFriendColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_EmailFriendList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}